﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Threading;
using DevExpress.XtraEditors;


namespace PRE.Catalog
{
    public partial class frmNhanvien : PRE.Catalog.FrmBase
    {
        //truy xuat du lieu
        DAL.Entities_DAL.Nhanvien_DAL _dal = new DAL.Entities_DAL.Nhanvien_DAL();
        BLL.Entities_BLL.Xu_ly_chung _xlc = new BLL.Entities_BLL.Xu_ly_chung();
        //Khai báo delegate
        public delegate void SendMessage(string Message);
        public SendMessage Sender;

        public frmNhanvien()
        {
            InitializeComponent();
            Sender = new SendMessage(GetMessage);
        }

        //them
        protected override void PerformAdd()
        {

            DataTable _dtb2 = _dal.Select_TenDN_notuse();
            gluTenDN.Properties.DataSource = _dtb2;
            //lay ma tu tang lon nhat
            DataTable _ma = _dal.Select_max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _xlc.AddID(_ma.Rows[0]["id"] + "");
                    txtMaNV.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);
                    ResetText();
                    txtHoten.Focus();
                }

            base.PerformAdd();
        }

        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        protected override void PerformDelete()
        {

            //click zo test de xoa
            string id = txtMaNV.Text;

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        protected override void PerformEdit()
        {
            DataTable _dtb2 = _dal.Select_TenDN_notuse();
            gluTenDN.Properties.DataSource = _dtb2;

            ReadOnlyControl(false);
            txtHoten.Focus();
            base.PerformEdit();
        }

        protected override void PerformPrint()
        {
            

            base.PerformPrint();
        }

        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            txtMaNV.Properties.ReadOnly = true;

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }
   
        protected override void PerformSave()
        {
            var ten = Proper(txtHoten.Text);
            var diachi = Proper(txtDiachi.Text);
            
            
            if (IsAdd)
            {
                var o = new DAL.Entities.NHAN_VIEN
                {
                    MA_NV = txtMaNV.Text,
                    HOTEN_NV = ten,
                    TEN_DANG_NHAP = gluTenDN.Text,
                    NGAY_SINH = Convert.ToDateTime(dtNgaysinh.EditValue),
                    MA_CV = txtChucvu.EditValue+"",
                    DIA_CHI = diachi,
                    DIEN_THOAI = txtSDT.Text,
                    GIOI_TINH = txtGT.Text
                };

                //them
                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            //sua
            if (IsEdit)
            {
                var _id = grvMain.GetFocusedRowCellValue("MA_NV") + "";

                var o = new DAL.Entities.NHAN_VIEN()
                {
                    MA_NV = txtMaNV.Text,
                    HOTEN_NV = ten,
                    TEN_DANG_NHAP = gluTenDN.Text,
                    NGAY_SINH = Convert.ToDateTime(dtNgaysinh.EditValue),
                    MA_CV = txtChucvu.EditValue + "",
                    DIA_CHI = diachi,
                    DIEN_THOAI = txtSDT.Text,
                    GIOI_TINH = txtGT.Text
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            base.PerformSave();
        }
    
        protected override void ResetText()
        {
            //bam nut them lam rong texbox
            txtHoten.Text = null;
            gluTenDN.Text = null;
            txtChucvu.Text = null;
            txtGT.Text = null;
            dtNgaysinh.EditValue = DateTime.Now;
            txtDiachi.Text = null;
            txtSDT.Text = null;
           
            base.ResetText();
        }

        protected override void ClearDataBindings()
        {
            txtMaNV.DataBindings.Clear();
            txtHoten.DataBindings.Clear();
            gluTenDN.DataBindings.Clear();
            txtChucvu.DataBindings.Clear();
            txtGT.DataBindings.Clear();
            dtNgaysinh.DataBindings.Clear();
            txtDiachi.DataBindings.Clear();
            txtSDT.DataBindings.Clear();
           
            base.ClearDataBindings();
        }

        protected override void DataBindingControl()
        {
            //show len texbox
            txtMaNV.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            txtHoten.DataBindings.Add("EditValue", _dtb, ".HOTEN_NV");
            gluTenDN.DataBindings.Add("EditValue", _dtb, ".TEN_DANG_NHAP");
            txtChucvu.DataBindings.Add("EditValue", _dtb, ".MA_CV");
            dtNgaysinh.DataBindings.Add("EditValue", _dtb, ".NGAY_SINH");
            txtDiachi.DataBindings.Add("EditValue", _dtb, ".DIA_CHI");
            txtGT.DataBindings.Add("EditValue", _dtb, ".GIOI_TINH");
            txtSDT.DataBindings.Add("EditValue", _dtb, ".DIEN_THOAI");
            

            base.DataBindingControl();
        }

        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //show len cho xem
            txtHoten.Properties.ReadOnly = isReadOnly;
            gluTenDN.Properties.ReadOnly = isReadOnly;
            txtChucvu.Properties.ReadOnly = isReadOnly;
            txtGT.Properties.ReadOnly = isReadOnly;
            txtDiachi.Properties.ReadOnly = isReadOnly;
            txtSDT.Properties.ReadOnly = isReadOnly;
            
            dtNgaysinh.Properties.ReadOnly = isReadOnly;

            dsNhanvien.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }
     
        protected override void LoadData()
        {
            //load len luoi data
            _dtb = _dal.Select();

            if (_dtb != null)
            {
                dsNhanvien.DataSource = _dtb;

                //dem so nhan vien duoi lable
                string _dem = _dal.Select_count();
                lbNhanvien.Text = _dem.ToString() + " thành viên";
            }

            base.LoadData();
        }
       
        public void GetMessage(string Message)
        {
            gluTenDN.Text = Message;
            label10.Text = Message;
        }

        private void frmNhanvien_Load(object sender, EventArgs e)
        {
            DataTable _dtb2 = _dal.Select_TenDN();
            gluTenDN.Properties.DataSource = _dtb2;

            DataTable _dtb4 = _dal.Select_CV();
            txtChucvu.Properties.DataSource = _dtb4;
        }

        //bay loi k cho nhap ky tu
        private static bool IsNumber(string val)
        {
            if (val != "")
                return Regex.IsMatch(val, @"^[0-9]*$");
            else return true;
        }
        //nt
        private void txtSDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((!char.IsNumber(e.KeyChar))&& (!e.Equals(Keys.Back))&& (!char.IsDigit(e.KeyChar))&&(!char.IsControl(e.KeyChar)))
            {
                MessageBox.Show("Lỗi! Chỉ nhập kí tự số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Handled = true;
            }           
        }     
        //bay loi tuoi phai nhap > 18 tuoi
        private void dtNgaysinh_Leave(object sender, EventArgs e)
        {
            DateTime _ngay = Convert.ToDateTime(dtNgaysinh.EditValue + "");
            string _tuoi = _dal.nhaptuoi(_ngay);
            if (Convert.ToInt32(_tuoi) < 18)
            {
                MessageBox.Show("Lỗi! Tuổi NV phải > 18 tuổi","Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }           
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            frmTaikhoan_load frmDN = new frmTaikhoan_load();
            frmDN.ShowDialog();
        }

        private void gluTenDN_MouseHover(object sender, EventArgs e)
        {
            DataTable _dtb3 = _dal.Select_TenDN_notuse();
            gluTenDN.Properties.DataSource = _dtb3;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            frmChucvu_load frmcvload = new frmChucvu_load();
            frmcvload.ShowDialog();
        }

        private void txtChucvu_MouseHover(object sender, EventArgs e)
        {
            DataTable _dtb4 = _dal.Select_CV();
            txtChucvu.Properties.DataSource = _dtb4;
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            var frm = new Report.Report_Base();
            var xr = new Reports.XR_Nhanvien();
            xr.DataSource = _dal.dsNhanvien();
            frm.SetReport(xr);
            frm.Show();
        }
    }
}
